package com.ys.controller.web.storage;

import com.ys.config.MenuId;
import com.ys.entity.storage.*;
import com.ys.enume.storage.TccFbaLogisticsMethodEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Author: fxz
 * @CreateTime: 2025-02-08
 * @Description: FBA发货单WEB
 * @Version: 1.5
 */
@Controller
@RequestMapping("/web/storage/tccFbaDeliver")
public class WebTccFbaDeliverController {
    private static final Logger logger = LoggerFactory.getLogger(WebTccFbaDeliverController.class);


    private final Long MENU_IDWl = MenuId.TCC_FbaLogisticsMethodFhWl;

    private final Long MENU_IDFee = MenuId.TCC_FbaFeeDetailsFhFy;

    @Autowired
    private CommonApiService<TccFbaLogisticsMethod> commonApiServiceWl;

    @Autowired
    private CommonApiService<TccFbaFeeDetails> commonApiServiceFee;

    /*
     * FBA发货物流方式详情
     */
    @GetMapping("/toEditWl")
    public String toEditWl(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
        try {
            Map<String, Object> tccFbaLogisticsMethod = new TccFbaLogisticsMethod().setSourceBid(bid).setSourceType(1)
                    .addField("case when A.nameLogsticsClearance = 0 then '自税' else '包税' end as nameLogsticsClearanceName")//清关方式
                    .addField("case when A.divisionalRule = 0 then '0.1kg进1kg' when A.divisionalRule = 1 then '0.5kg进1kg' else '不约分' end as divisionalRuleName")//约分规则
                    .addField("FORMAT(A.createTime, 'yyyy-MM-dd HH:mm:ss') createTime1")//创建时间
                    .addField("A.sourceBid fbaDeliverBid")//FBA发货单号
                    .addLeftJoin("TCC_FbaDeliver DD ON DD.Bid = A.sourceBid")
                    .addLeftJoin("TCC_FbaPlan B ON B.Bid = DD.FbaPlanBid")
                    .addField("B.bid bid,B.Vgsbm vgsbm,B.VgsJc vgsJc ")
                    .setSourceType(TccFbaLogisticsMethodEnum.SourceType.FBAFH.value).queryFirstMap();
            commonApiServiceWl.toEdit(model, Long.valueOf(tccFbaLogisticsMethod.get("id").toString()), new TccFbaLogisticsMethod(), MENU_IDWl, tccFbaLogisticsMethod, null);
            return "storage/tccFbaPlan/toEditWl";
        }catch (Exception e) {
            logger.error("FBA发货物流方式详情页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 预估费用详情
     */
    @GetMapping("/toEditFee")
    public String toEditFee(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
        try {
            Map<String, Object> tccFbaFeeDetails = new TccFbaFeeDetails().setSourceBid(bid).setSourceType(1)
                    .setSourceType(TccFbaLogisticsMethodEnum.SourceType.FBAFH.value)
                    .addField("A.sourceBid fbaDeliverBid")//FBA发货单号
                    .addLeftJoin("TCC_FbaDeliver DD ON DD.Bid = A.sourceBid")
                    .addLeftJoin("TCC_FbaPlan B ON B.Bid = DD.FbaPlanBid")
                    .addField("B.bid bid,B.Vgsbm vgsbm,B.VgsJc vgsJc,B.ShopName shopName")
                    .addLeftJoin("YS_ProcInst P ON P.Bid = B.Bid ")
                    .addField("FORMAT(A.createTime, 'yyyy-MM-dd HH:mm:ss') createTime1")//创建时间
                    .addLeftJoin("TCC_FbaLogisticsMethod C ON C.SourceBid = A.sourceBid AND C.SourceType = 1")
                    .addField("C.LogsticsWay logsticsWay,C.Cck cck,C.nameLogsticsClearance nameLogsticsClearance")
                    .addField("C.transportationCn transportationCn,C.remark remarkWl,C.vck vck,C.clearanceOpeAmount clearanceOpeAmount,C.jfFee jfFee")
                    .addField("C.pzFee pzFee")
                    .addField("C.additionalBilling")
                    .addField("case when (A.numProName - C.FreeProNameNum) < 0 then 0 else A.numProName - C.FreeProNameNum end as numProNameSf")
                    .addField("case when C.nameLogsticsClearance = 0 then '自税' else '包税' end as nameLogsticsClearanceName")//清关方式
                    .addField("CAST('实际物流费（总）=实际计重费（￥）+实际附加费（￥）+清关服务费(￥)' AS VARCHAR(255))  priceTranPredictSm")
                    .queryFirstMap();
            commonApiServiceFee.toEdit(model, Long.valueOf(tccFbaFeeDetails.get("id").toString()), new TccFbaFeeDetails(), MENU_IDFee, tccFbaFeeDetails, null);
            return "storage/tccFbaPlan/toEditWl";
        }catch (Exception e) {
            logger.error("预估费用详情页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 计划审核-按SKU自动发货
     */
    @GetMapping("/flow")
    public String flow(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
        try {
            model.addAttribute("bid",bid);
            return "storage/tccFbaPlan/toFlow";
        }catch (Exception e) {
            logger.error("计划审核页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 计划审核-按SKU自动发货-详情
     */
    @GetMapping("/flowDetail")
    public String flowDetail(Model model, HttpServletRequest request, HttpServletResponse response, String bid,String shipmentId) {
        try {
            model.addAttribute("bid",bid);
            model.addAttribute("shipmentId",shipmentId);
            return "storage/tccFbaPlan/toFlowDetail";
        }catch (Exception e) {
            logger.error("计划审核页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }
}

